<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/9
 * Time: 18:24
 */
namespace Libraries;

class Form {
	/**
	 * 编辑器
	 * 支持多文本框编辑
	 *
	 * @param string $textareaid
	 * @param string $value 默认输入值
	 * @param string $toolbar    有basic full 和standard三种
	 * @param int $height 编辑器高度
	 * @param string $color 编辑器颜色
	 * @param bool $up 是否允许上传
	 */
	public static function editor($textareaName = 'content', $value = '', $toolbar = 'standard', $height = 400, $color = '', $up=true)
	{
		$str ='<textarea name="'.$textareaName.'">'.$value.'</textarea>';
		if(!defined('IS_CKEDITOR_LOADED')) {
			$str .= '<script type="text/javascript" src="/public/js/ckeditor/ckeditor.js"></script>';
			define('IS_CKEDITOR_LOADED', 1);
		}
		if ($up) {
			if (!defined('IS_CKFINDER_LOADED'))	{
				$str .= '<script type="text/javascript" src="/public/js/ckfinder/ckfinder.js"></script>';
				define('IS_CKFINDER_LOADED', 1);
			}
		}
		$str .= "<script type=\"text/javascript\">\r\n";
		if ($up) {
			$str .= "var editor = CKEDITOR.replace('".$textareaName."',{\r\n";
		} else {
			$str .= "CKEDITOR.replace('" . $textareaName . "', {\r\n";
		}
		// toolbar 设定只能用独立的JS文件设定，这里三个JS文件为自定义的JS文件
		if ($toolbar == 'basic') {
			$str .= "customConfig: '/public/js/ckeditor/config_toolbar_basic.js',\r\n";
		}elseif($toolbar == 'standard')	{
			$str .= "customConfig: '/public/js/ckeditor/config_toolbar_standard.js',\r\n";
		}elseif($toolbar == 'full')	{
			$str .= "customConfig: '/public/js/ckeditor/config_toolbar_full.js',\r\n";
		}else {
			$str .= "customConfig: '/public/js/ckeditor/config.js',\r\n";
		}
		$str .= "height: " . $height . ",\r\n";
		if ($color) {
			$str .= "uiColor: '" . $color . "',\r\n";
		}
		$str .= "});\r\n";
		if ($up) {
			$str .= "CKFinder.setupCKEditor( editor, '/public/js/ckfinder/' );\r\n";
		}
		$str .= '</script>';
		return $str;
	}

	/**
	 * ckFinder上传控件
	 * 支持多文件上传
	 *
	 * @param string $inputName 控件name, id
	 * @param string $inputValue input初始值
	 * @param string $inputClass input输入框样式
	 * @param string $buttonClass button样式
	 * @param string $thumbValue 缩略图初始值
	 * @param bool $thumbnail 是否查看缩略图
	 */
	public static function ckFinder($inputName = 'uploadImgPath', $inputValue = '', $inputClass='', $buttonClass='', $thumbValue='', $thumbnail = true)
	{
		$str = '<input type="text" id="'.$inputName.'" name="'.$inputName.'" class="'.$inputClass.'" value="'.$inputValue.'" />';
		$str .= '<input type="button" class="'.$buttonClass.'" value="+ 浏览上传" onclick="BrowseServer(\''.$inputName.'\');" />';
		if (!$thumbnail){
			$str .= '<div style="display:none;"><img id="'.$inputName.'_randomId" src="'.$thumbValue.'" border="0" /></div>';
		}else{
			$str .= '<div style="float: left;padding-left: 20px;"><img id="'.$inputName.'_randomId" src="'.$thumbValue.'" border="0" /></div>';
		}
		if(!defined('IS_CKFINDER_LOADED'))
		{
			$str .= '<script type="text/javascript" src="/public/js/ckfinder/ckfinder.js"></script>';
			define('IS_CKFINDER_LOADED', 1);
		}
		if (!defined('IS_MULTI_CKFINDER_INPUT')){
			$str .= '<script type="text/javascript">
					function BrowseServer(inputId) {
						var finder = new CKFinder();
						finder.basePath="/public/upload/";    //导入CKFinder的路径
						finder.selectActionFunction = SetFileField; //设置文件被选中时的函数
						finder.selectActionData=inputId;   //接收地址的input ID
						finder.callback = function (api) { finderAPI = api; };  //通过api读取缩略图\';
						finder.popup(); //然后弹出浏览文件的窗口
					}
					function SetFileField( fileUrl, data) {
						// 通过api读取缩略图
						document.getElementById(data["selectActionData"]+\'_randomId\').src = finderAPI.getSelectedFile().getThumbnailUrl();
						document.getElementById(data["selectActionData"]).value = fileUrl;
					}
					</script>';
			define('IS_MULTI_CKFINDER_INPUT',1);
		}
		return $str;
	}



	/**
	 * 日期时间控件
	 *
	 * @param $name 控件name，id
	 * @param $value 选中值
	 * @param $isdatetime 是否显示时间
	 * @param $loadjs 是否重复加载js，防止页面程序加载不规则导致的控件无法显示
	 * @param $showweek 是否显示周，使用，true | false
	 */
	public static function date($name, $value = '', $isdatetime = 0, $loadjs = 0) {
		if($value == '0000-00-00 00:00:00') $value = '';
		$id = preg_match("/\[(.*)\]/", $name, $m) ? $m[1] : $name;
		if($isdatetime) {
			$size = 21;
			$format = '%Y-%m-%d %H:%M:%S';
			$showsTime = 12;
		} else {
			$size = 10;
			$format = '%Y-%m-%d';
			$showsTime = 'false';
		}
		$str = '';
		if($loadjs || !defined('CALENDAR_INIT')) {
			define('CALENDAR_INIT', 1);
			$str .= '<script src="/public/js/date/js/jscal2.js"></script>
   				 <script src="/public/js/date/js/lang/cn.js"></script>
   				 <link rel="stylesheet" type="text/css" href="/public/js/date/css/jscal2.css" />
    				<link rel="stylesheet" type="text/css" href="/public/js/date/css/border-radius.css" />
    				<link rel="stylesheet" type="text/css" href="/public/js/date/css/plain.css" />';
		}
		$str .= '<input type="text" name="'.$name.'" id="'.$id.'" value="'.$value.'" size="'.$size.'" class="date input w50" readonly>&nbsp;';
		$str .= '<script type="text/javascript">
			Calendar.setup({
			weekNumbers: true,
		    inputField : "'.$id.'",
		    trigger    : "'.$id.'",
		    dateFormat: "'.$format.'",
		    showTime: '.$showsTime.',
		    minuteStep: 1,
		    onSelect   : function() {this.hide();}
			});
        </script>';
		return $str;
	}

	/**
	 * 颜色控件
	 *
	 * @param $name 控件name
	 * @param $value 选中值
	 */
	public static function color($name, $value = '000000') {

		if(!defined('COLOR_INIT')) {
			define('COLOR_INIT', 1);
			$str= '<script src="'.B_PUBLIC.'/js/jscolor/jscolor.js"></script>';

		}
		$str .= '<input class="color" style="width:48px;height:16px;overfrom:hidden" name="'.$name.'" value="'.$value.'" />';

		return $str;
	}

}
